package com.qike.baoxiu.controller;

import com.qike.baoxiu.config.PassToken;
import com.qike.baoxiu.dto.*;
import com.qike.baoxiu.pojo.Directory;
import com.qike.baoxiu.pojo.MonthReport;
import com.qike.baoxiu.pojo.User;
import com.qike.baoxiu.pojo.UserDevice;
import com.qike.baoxiu.response.BaseResult;
import com.qike.baoxiu.response.RetResponse;
import com.qike.baoxiu.service.OrderService;
import com.qike.baoxiu.service.UserService;
import com.qike.baoxiu.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@CrossOrigin
@Api(description = "后台接口")
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;


    /**
     * 登录
     *
     * @param loginDto
     * @return
     */
    @ApiOperation("登录")
    @PostMapping("/Login")
    @ResponseBody
    @PassToken
    public BaseResult Login(@RequestBody LoginDto loginDto) {
        try {
            LoginVo vo = userService.login(loginDto);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 列表头部统计数据
     *
     * @return
     */
    @ApiOperation("列表头部统计数据")
    @GetMapping("/count")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    @ResponseBody
    public BaseResult count() {
        try {
            OrderCountVo orderCountVo = orderService.OrderCount();
            return RetResponse.makeOKRsp(orderCountVo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 维修单列表
     *
     * @return
     */
    @ApiOperation("维修单列表")
    @GetMapping("/orderList")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    @ResponseBody
    public BaseResult orderList(OrderListDto orderListDto) {
        try {
            OrderListVo vo = orderService.orderList(orderListDto);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 维修单详情
     *
     * @return
     */
    @ApiOperation("维修单详情")
    @GetMapping("/orderInfo")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult orderInfo(Integer id) {
        try {
            OrderInfoVo vo = orderService.orderInfo(id);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 添加维修单
     *
     * @return
     */
    @ApiOperation("添加维修单")
    @PostMapping("/addOrder")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult addOrder(@RequestBody AddOrderDto addOrderDto) {
        try {
            orderService.addOrder(addOrderDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 人员管理列表
     *
     * @return
     */
    @ApiOperation("人员管理列表")
    @GetMapping("/fixUserList")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult fixUserList(FixUserListDto fixUserListDto) {
        try {
            FixUserListVo vo = orderService.fixUserList(fixUserListDto);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 工程师列表
     *
     * @return
     */
    @ApiOperation("工程师列表")
    @GetMapping("/engineList")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult engineList(String name) {
        try {
            List<User> vo = orderService.engineList(name);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 用户管理列表
     *
     * @return
     */
    @ApiOperation("用户管理列表")
    @GetMapping("/UserList")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult UserList(FixUserListDto fixUserListDto) {
        try {
            FixUserListVo vo = orderService.UserList(fixUserListDto);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 下拉列表
     *
     * @return
     */
    @ApiOperation("下拉列表  role 角色 1 工程师 2 用户")
    @GetMapping("/UserListMenu")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult UserListMenu(String phone, Integer role) {
        try {
            List<UserListMenuVo> vo = orderService.UserListMenu(phone, role);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 停用用户
     *
     * @return
     */
    @ApiOperation("人员启用/停用")
    @PostMapping("/delFixUser")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult delFixUser(@RequestBody Integer id) {
        try {
            orderService.delUser(id);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 添加人员
     *
     * @return
     */
    @ApiOperation("添加人员")
    @PostMapping("/addFixUser")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult addFixUser(@RequestBody AddFixUserDto addFixUserDto) {
        try {
            orderService.addFixUser(addFixUserDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 添加用户
     *
     * @return
     */
    @ApiOperation("添加用户")
    @PostMapping("/addUser")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult addUser(@RequestBody AddUserDto addUserDto) {
        try {
            orderService.addUser(addUserDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 报修历史
     *
     * @return
     */
    @ApiOperation("报修历史")
    @GetMapping("/orderHistory")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult orderHistory(Integer userId) {
        try {
            List<OrderHistoryVo> orderHistoryVos = orderService.orderHistory(userId);
            return RetResponse.makeOKRsp(orderHistoryVos);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 设备管理详情
     *
     * @return
     */
    @ApiOperation("设备管理详情")
    @GetMapping("/deviceInfo")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult deviceInfo(Integer userId) {
        try {
            UserDevice vo = orderService.deviceInfo(userId);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 设备管理编辑
     *
     * @return
     */
    @ApiOperation("设备管理编辑")
    @PostMapping("/deviceEdit")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult deviceEdit(@RequestBody UserDevice userDevice) {
        try {
            orderService.deviceEdit(userDevice);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 知识库列表
     *
     * @return
     */
    @ApiOperation("知识库列表")
    @GetMapping("/knowledgeList")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult knowledgeList(KnowledgeListDto knowledgeListDto) {
        try {
            KnowledgeListVo vo = orderService.knowledgeList(knowledgeListDto);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 添加知识库
     *
     * @return
     */
    @ApiOperation("添加知识库")
    @GetMapping("/knowledgeAdd")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult knowledgeAdd(KnowledgeAddDto knowledgeListDto) {
        try {
            orderService.knowledgeAdd(knowledgeListDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 删除知识库
     *
     * @return
     */
    @ApiOperation("删除知识库")
    @PostMapping("/knowledgeDel")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult knowledgeDel(@RequestBody Integer id) {
        try {
            orderService.knowledgeDel(id);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 解决方案列表
     *
     * @return
     */
    @ApiOperation("解决方案列表")
    @GetMapping("/fixList")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult fixList(FixListDto fixListDto) {
        try {
            FixListVo fixListVo = orderService.fixList(fixListDto);
            return RetResponse.makeOKRsp(fixListVo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 解决方案变更状态
     *
     * @return
     */
    @ApiOperation("解决方案变更状态 '0 未确认 1 确认 2 忽略")
    @PostMapping("/fixOrderChangeStatus")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
//    public BaseResult fixOrderChangeStatus(@RequestBody Integer id,Integer status) {
    public BaseResult fixOrderChangeStatus(@RequestBody FixOrderChangeStatus fixOrderChangeStatus) {
        try {
            orderService.fixOrderChangeStatus(fixOrderChangeStatus.getId(), fixOrderChangeStatus.getStatus());
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 上传图片
     *
     * @return
     */
    @ApiOperation("上传图片")
    @PostMapping("/upLoadPhoto")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult upLoadPhoto(@RequestBody MultipartFile file) {
        try {
            String url = orderService.upLoadPhoto(file);
            return RetResponse.makeOKRsp(url);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 保修单统计报表
     *
     * @return
     */
    @ApiOperation("保修单统计报表")
    @GetMapping("/fixOrderCount")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult fixOrderCount(FixOrderCountDto fixOrderCountDto) {
        try {
            List<FixOrderCountVo> list = orderService.fixOrderCount(fixOrderCountDto);
            return RetResponse.makeOKRsp(list);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 来源渠道统计报表
     *
     * @return
     */
    @ApiOperation("来源渠道统计报表")
    @GetMapping("/fromCount")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult fromCount(FromCountDto fromCountDto) {
        try {
            List<FromCountVo> list = orderService.fromCount(fromCountDto);
            return RetResponse.makeOKRsp(list);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 平均响应时间统计报表
     *
     * @return
     */
    @ApiOperation("平均响应时间统计报表")
    @GetMapping("/avgResponseTime")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult avgResponseTime(FixOrderCountDto fixOrderCountDto) {
        try {
            List<FixOrderCountVo> list = orderService.avgResponseTimeCount(fixOrderCountDto);
            return RetResponse.makeOKRsp(list);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 平均完成时间统计报表
     *
     * @return
     */
    @ApiOperation("平均完成时间统计报表")
    @GetMapping("/avgFinishTime")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult avgFinishTime(FixOrderCountDto fixOrderCountDto) {
        try {
            List<FixOrderCountVo> list = orderService.avgFinishTimeCount(fixOrderCountDto);
            return RetResponse.makeOKRsp(list);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 评价统计报表
     *
     * @return
     */
    @ApiOperation("评价统计报表")
    @GetMapping("/scoreCount")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)})
    public BaseResult scoreCount(ScoreCountDto scoreCountDto) {
        try {
            List<ScoreCountMapVo> list = orderService.scoreCount(scoreCountDto);
            return RetResponse.makeOKRsp(list);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 评价已完成维修单
     *
     * @return
     */
    @ApiOperation("评价已完成维修单 如果已经评价 会返回分数 提交按钮置灰  否则会返回空")
    @PostMapping("/scoreFinishOrder")
    @ResponseBody
    public BaseResult scoreFinishOrder(ScoreFinishOrderDto scoreFinishOrderDto) {
        try {
            Integer score = orderService.scoreFinishOrder(scoreFinishOrderDto);
            return RetResponse.makeOKRsp(score);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 父字典列表
     *
     * @return
     */
    @ApiOperation("父字典列表")
    @GetMapping("/directoryList")
    @ResponseBody
    public BaseResult directoryList(String name) {
        try {
            List<Directory> nameList = orderService.directoryList(name);
            return RetResponse.makeOKRsp(nameList);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 子字典列表
     *
     * @return
     */
    @ApiOperation("子字典列表")
    @GetMapping("/sonDirectoryList")
    @ResponseBody
    public BaseResult sonDirectoryList(Integer parentId) {
        try {
            List<Directory> nameList = orderService.sonDirectoryList(parentId);
            return RetResponse.makeOKRsp(nameList);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 全部字典列表
     *
     * @return
     */
    @ApiOperation("全部字典列表")
    @GetMapping("/allDirectoryList")
    @ResponseBody
    public BaseResult allDirectoryList() {
        try {
            List<AllDirectoryListVo> nameList = orderService.allDirectoryList();
            return RetResponse.makeOKRsp(nameList);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 软件名称列表
     *
     * @return
     */
    @ApiOperation("软件名称列表")
    @GetMapping("/softNameList")
    @ResponseBody
    public BaseResult softNameList() {
        try {
            List<String> nameList = orderService.softNameList();
            return RetResponse.makeOKRsp(nameList);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 添加字典
     *
     * @return
     */
    @ApiOperation("添加字典")
    @GetMapping("/addDirectory")
    @ResponseBody
    public BaseResult addDirectory(AddDirectoryDto addDirectoryDto) {
        try {
            orderService.addDirectory(addDirectoryDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 删除字典
     *
     * @return
     */
    @ApiOperation("删除字典")
    @DeleteMapping("/delDirectory")
    @ResponseBody
    public BaseResult delDirectory(Integer id) {
        try {
            orderService.delDirectory(id);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 查看月报
     *
     * @return
     */
    @ApiOperation("查看月报")
    @DeleteMapping("/ShowMonthReport")
    @ResponseBody
    public BaseResult ShowMonthReport(Integer uid) {
        try {
            MonthReport monthReport = orderService.ShowMonthReport(uid);
            return RetResponse.makeOKRsp(monthReport);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }
}


